package com.flyone.fuxiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyone.fuxiao.common.ErrorCode;
import com.flyone.fuxiao.constant.CommonConstant;
import com.flyone.fuxiao.exception.BusinessException;
import com.flyone.fuxiao.model.dto.note.NoteQueryRequest;
import com.flyone.fuxiao.model.dto.note.NoteSaveRequest;
import com.flyone.fuxiao.model.entity.UserNote;
import com.flyone.fuxiao.model.entity.baidu.ATA;
import com.flyone.fuxiao.model.entity.baidu.ATAItem;
import com.flyone.fuxiao.service.UserNoteService;
import com.flyone.fuxiao.mapper.UserNoteMapper;
import com.flyone.fuxiao.utils.AipNIpUtils;
import com.flyone.fuxiao.utils.SqlUtils;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author luoyifei
* @description 针对表【user_note(用户笔记表)】的数据库操作Service实现
* @createDate 2024-04-19 17:08:55
*/
@Service
public class UserNoteServiceImpl extends ServiceImpl<UserNoteMapper, UserNote>
    implements UserNoteService{

    @Resource
    private AipNIpUtils aipNIpUtils;

    @Override
    public long saveWrite(NoteSaveRequest noteSaveRequest, Long userId) {
        try {
            Preconditions.checkArgument(userId != null, "userId不能为空");
            Preconditions.checkArgument(StringUtils.isNotBlank(noteSaveRequest.getTitle()), "标题不能为空");
            Preconditions.checkArgument(StringUtils.isNoneBlank(noteSaveRequest.getContent()), "内容不能为空");
        } catch (Exception e){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, e.getMessage());
        }
        String originContent = noteSaveRequest.getOriginContent();
        String accessToken = aipNIpUtils.getAccessToken();
        ATA ata = aipNIpUtils.sentimentClassify(originContent, accessToken);
        List<ATAItem> items = ata.getItems();
        UserNote userNote = new UserNote();
        userNote.setUserId(userId);
        userNote.setOriginContent(originContent);
        userNote.setSentiment(items.get(0).getSentiment());
        userNote.setConfidence(items.get(0).getConfidence());
        userNote.setPositiveProb(items.get(0).getPositive_prob());
        userNote.setNegativeProb(items.get(0).getNegative_prob());
        BeanUtils.copyProperties(noteSaveRequest, userNote);
        if(ObjectUtils.isEmpty(userNote.getId())){
            save(userNote);
        }else {
            updateById(userNote);
        }
        return userNote.getId();
    }

    @Override
    public QueryWrapper<UserNote> getQueryWrapper(NoteQueryRequest noteSaveRequest) {
        QueryWrapper<UserNote> queryWrapper = new QueryWrapper<>();
        String searchText = noteSaveRequest.getSearchText();
        String title = noteSaveRequest.getTitle();
        String content = noteSaveRequest.getContent();
        String sortField = noteSaveRequest.getSortField();
        String sortOrder = noteSaveRequest.getSortOrder();

        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.like("title", searchText).or().like("content", searchText);
        }
        if (StringUtils.isNotBlank(title)) {
            queryWrapper.like("title", title);
        }
        if (StringUtils.isNotBlank(content)) {
            queryWrapper.like("originContent", content);
        }
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);

        return queryWrapper;
    }
}




